package com.dongdongshop.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlidyConfig {

    @Value("${alidy.region}")
    private String region;
    @Value("${alidy.endpointOverride}")
    private String endPoint;

    @Value("${alidy.accessKeyId}")
    private String accessKeyId;

    @Value("${alidy.accessKeySecret}")
    private String accessKeySecret;



    @Bean
    public AsyncClient asyncClient(@Autowired StaticCredentialProvider provider) {
        AsyncClient client = AsyncClient.builder()
                .region(region) // Region ID
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(endPoint)
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
        return client;
    }
    @Bean
    public StaticCredentialProvider provider() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                //.securityToken("<your-token>") // use STS token
                .build());
        return provider;
    }
}
